package com.sifeng.test;

import com.sifeng.utils.MyUtils;
import com.sifeng.utils.SqlUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * @ClassName StringConcatenation
 * @Author yang_gsh
 * @Date 2019-3-30 9:06
 * @Description
 */
public class StringConcatenation {

    public static String stringCon(String sentence, ArrayList list) {
        StringBuffer result = new StringBuffer();
/*		int off = false;
		int idx = false;*/
        int k = 0;
        ArrayList list1 = (ArrayList)list.clone();
        ArrayList delIdxList = new ArrayList();

        int i;
        for(i = 0; i < sentence.length(); ++i) {
            if(sentence.charAt(i) == 63) {
                result.append(list1.get(k));
                ++k;
            }else {
                result.append(sentence.charAt(i));
            }
        }

        list.clear();

        for(i = 0; i < list1.size(); ++i) {
            if(!delIdxList.contains(Integer.valueOf(i))) {
                list.add(list1.get(i));
            }
        }

        return result.toString();
    }

    public static void writeContent(String path, String content) throws IOException {
        FileWriter writer = new FileWriter(path);
        writer.write(content);
        writer.flush();
        writer.close();
    }

    public static void createSentence(String excelPath, String sentence, String outFilePath) throws IOException {
        String content = "";
        XSSFWorkbook wb = MyUtils.getXSSFWorkbook(excelPath);
        XSSFSheet sheet = wb.getSheetAt(0);
        if (sheet == null) {
            return;
        }

        for (Row row : sheet) {
            if(row == null) {
                continue;
            }
            ArrayList<Object> list = new ArrayList<Object>();
            for (Cell cell : row) {
                if(cell == null) {
                    list.add("");
                } else {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    list.add(cell.getStringCellValue());
                }
            }

            content += stringCon(sentence, list) + "\n";
        }

        writeContent(outFilePath, content);
    }

    public static void main(String[] args) throws IOException {
        String sentence = "sendmsg OMKMPVP1 66802001 -d \"SYS_CNL=SYS&BUS_CNL=SYS&THD_JRN=SP28?&" +
                "MBL_NO=?&CR_AMT=?&MKT_ID=?&PRD_ID=?&SYS_CNL=SP28&PRVP_CD=28210100&SMS_FLG=0&OPTYP=0\"";
        String excelPath = "E:\\testt.xlsx";
        String outFilePath = "E:\\和包券补发命令.txt";
        StringConcatenation.createSentence(excelPath, sentence, outFilePath);
    }
}
